<?php
namespace app\api\model;
use think\Cache;
use controller\Easemob;

class Supplier extends Common
{
    /**
     * 用户注册
     */
    public function register($data)
    {
        $name = $data['name'];
        $mobile  = $data['mobile'];

        $user = $this->where('mobile',$mobile)->where('state','<>','2')->find();
        if($user) {return 500;}; 
        $user = $this->where('name',$name)->where('state','<>','2')->find();
        if($user) {return 501;}; 
        if($this->data($data)->allowField(true)->save() !== false)return 0;

        return 503;
    }

    /**
     * 用户登录认证
     */
    public function login($mobile, $password){
        $map['mobile'] = $mobile;
        /* 获取用户数据 */
        $user = $this->where($map)->find();
        $userAuth = model('RealAuth')->get(['type'=>1,'user_id'=>$user['id'],'state'=>1]);

        if($user){
            //:0=审核中,1=审核通过,2=审核未通过
            /* 验证用户密码 */
            if($password==Cache::get($mobile))
            {
                // 生成 token
                $token = buildToken($user->name);
                Cache::set(config('tokenPrefix') . $token, $user->id, config('token_expires'));
                $is_realauth = $is_bank = $is_transpwd = 0;
                if($userAuth)
                {
                    $is_realauth = 1;
                    if($userAuth['card_no'])$is_bank = 1;
                    if($userAuth['transpwd'])$is_transpwd = 1;
                }
                $data = [
                    'token' => $token,
                    'type'  =>  $user['type'],
                    'state' => $user['state'],
                    'mobile'=> $user['mobile'],
                    'name'  => $user['name'],
                    'is_realauth' => $is_realauth,
                    'is_bank' => $is_bank,
                    'is_transpwd' => $is_transpwd,
                    'avatar'  => $user['avatar'],
                ];
                return $data; //登录成功，返回用户ID
            } 
            else 
            {
                return 0; //密码错误
            }
        }
        else 
        {
            return 0; //用户不存在或被禁用
        }
    }

    public function logout()
    {
        cache(config('tokenPrefix').$this->token, NULL);
        cache(config('tokenAndUserPrefix') . $user['id'], NULL);
    }

    public function getUserInfo($token)
    {
        $user_id = Cache::get(config('tokenPrefix') . $token);

        if ($user_id == false) return false;

        $user = $this->where('id',$user_id)->find();
        
        return $user;
    }

    public function getSitetype($type)
    {
        switch ($type) {
                   case '1':
                       $sitetypeString .= '  商务会议';
                       break;
                   case '2':
                       $sitetypeString .= '  酒店宴席';
                       break;
                   case '3':
                       $sitetypeString .= '  专业会议策划';
                       break;
                   case '4':
                       $sitetypeString .= '  教育培训';
                       break;
                   case '5':
                       $sitetypeString .= '  公司年会';
                       break;
                   default:
                       $sitetypeString .= '  广告发布';
                       break;
        }
    }
}